﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */


using System.Collections.Generic;
using System.Linq;

using Twipler.App.Parse.Code;
using Twipler.App.Render.Tree;
using Twipler.App.Machine;

namespace Twipler.App.Parse.Html.Consumers
{
    [Consumer(OpenTag="field", Parameters=typeof(FieldConsumerParameters))]
    public class FieldConsumer : TemplateConsumer
    {
        public class FieldConsumerParameters : TemplateParameters
        {
            [HtmlArgument(TokenParser.TokenType.StaticMethodCall, true)]
            public StaticMethodCall Source = null;

            [HtmlArgument(TokenParser.TokenType.InstructionList, true)]
            public List<Instruction> Program = null;

            [HtmlArgument(TokenParser.TokenType.Any, true)]
            public string Name = null;

            [HtmlArgument(TokenParser.TokenType.Any, true)]
            public string Field = null;
        }

        public FieldConsumer()
        {
            Parameters = new FieldConsumerParameters();
        }

        public override AbstractItem Process(Template template, TemplateConsumer parent)
        {
            ReadArguments(template);
            if (template.HasErrors)
                return ParseErrorItem.Create(template.Errors.First().Message);

            template.SkipWhiteSpace();
            if (template.Peek(Template.EndMarker, false) == false)
                return Error(template, "Missing template close tag (" + Template.EndMarker + ")");

            template.SkipOver(Template.EndMarker);


            return App.Render.Tree.FieldItem.Create((FieldConsumerParameters)Parameters);
        }
    }
}